/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.bean;

import com.spakehelpdesk.entity.Status;
import com.spakehelpdesk.entity.Account;
import com.spakehelpdesk.entity.Category;
import com.spakehelpdesk.entity.Complaint;
import com.spakehelpdesk.java.MailConfig;
import com.spakehelpdesk.java.MessageManage;
import com.spakehelpdesk.java.SessionManage;
import com.spakehelpdesk.java.URLManage;
import com.spakehelpdesk.session.AccountSession;
import com.spakehelpdesk.session.CategorySession;
import com.spakehelpdesk.session.ComplaintSession;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author leehoanh
 */
@SessionScoped
public class CategoryBean implements Serializable {
    @EJB
    private AccountSession accountSession;

    @EJB
    private ComplaintSession complaintSession;
    @EJB
     
    private CategorySession categorySession;
    private List<Category> listCategory;
    private Category selectCategory;
    private Category newCategory = new Category();
    private Complaint newComplaint = new Complaint();


    public Complaint getNewComplaint() {
        newComplaint.setCategoryID(new Category(1));
        return newComplaint;
    }

    public void setNewComplaint(Complaint newComplaint) {
        this.newComplaint = newComplaint;
    }

    public CategorySession getCategorySession() {
        return categorySession;
    }

    public void setCategorySession(CategorySession categorySession) {
        this.categorySession = categorySession;
    }
    private List<Category> categoryList = new ArrayList<Category>();

    public List<Category> getListCategory() {
        return listCategory;
    }

    public List<Category> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    public void setListCategory(List<Category> listCategory) {
        this.listCategory = listCategory;
    }

    public Category getNewCategory() {
        return newCategory;
    }

    public AccountSession getAccountSession() {
        return accountSession;
    }

    public void setAccountSession(AccountSession accountSession) {
        this.accountSession = accountSession;
    }

    public ComplaintSession getComplaintSession() {
        return complaintSession;
    }

    public void setComplaintSession(ComplaintSession complaintSession) {
        this.complaintSession = complaintSession;
    }
    

    public void setNewCategory(Category newCategory) {
        this.newCategory = newCategory;
    }

    public Category getSelectCategory() {
        return selectCategory;
    }
    
    public void setSelectCategory(Category selectCategory) {
        this.selectCategory = selectCategory;
    }
    //Get all category
    public List<Category> getCategorys() {
        if (SessionManage.getValue("loadcategory") == null) {
            listCategory = categorySession.getAll();
            SessionManage.setValue("loadcategory", "loaded");
        }
        System.out.println("***********8" + listCategory.size());

        return listCategory;
    }
    //navigate to ManageDeprtment page

    public void back() {
        //Check the exist of session loaddepartment
        if (SessionManage.getValue("loadcategory") != null) {
            SessionManage.remove("loadcategory");
        }

        URLManage.sendRediret("Admin/ManageCategory.xhtml");
    }
    
    //Get all category
    public List<Category> getAll() {
        categoryList = categorySession.getAll();
        return categoryList;
    }
    //Create d new department

    public void createNew() {
        categorySession.createNewCategory(newCategory);
        MessageManage.message(FacesMessage.SEVERITY_INFO, "Create successful!", "Successful!");
        newCategory = new Category();
        if (SessionManage.getValue("loadcategory") != null) {
            SessionManage.remove("loadcategory");
        }
    }
    //update category
    public void update() {
        selectCategory = categorySession.updateCategory(selectCategory);
        if (SessionManage.getValue("loadcategory") != null) {
            SessionManage.remove("loadcategory");
        }

    }
    //delete category
    public void delete() {
        categorySession.remove(selectCategory);
        if (SessionManage.getValue("loadcategory") != null) {
            SessionManage.remove("loadcategory");
        }
    }
    //create complaint
    public void createComplaint() {
        System.out.println("*************************" + selectCategory.getCategoryID());
        newComplaint.setAccountID(new Account(SessionManage.getValue("accountId").toString()));
        newComplaint.setCategoryID(new Category(selectCategory.getCategoryID()));
        newComplaint.setDateLodge(new Date());
        newComplaint.setStatus(new Status("PEND"));

    }
    
    //create
    public void create() {
        try {
            Account newAccount = accountSession.getAccountById(SessionManage.getValue("accountId").toString());
            int count = complaintSession.getComplaintByAccount(SessionManage.getValue("accountId").toString()).size();
            String comCode = "COMPU000"+ String.valueOf(count)+1;
            newComplaint.setComplaintNo(comCode);
            complaintSession.createNewComplaint(newComplaint);
            MailConfig.send("smtp.gmail.com", "congsonit@gmail.com", newAccount.getEmail().toString(), passWord,newComplaint.getNote().toString(), newComplaint.getDescription().toString());
        } catch (Exception ex) {
            Logger.getLogger(CategoryBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    //select category name by category ID
    public SelectItem[] getCategory() {
        List<Category> cate = categorySession.getAll();
        SelectItem[] category = null;
        if (cate != null) {
            category = new SelectItem[cate.size() + 1];
            category[0] = new SelectItem(" ", "--Select--");
            for (int i = 0; i < cate.size(); i++) {
                category[i + 1] = new SelectItem(cate.get(i).getCategoryID(), cate.get(i).getCategoryName());
            }
        }
        return category;
    }
    
    private String passWord;

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    
    /**
     * Creates a new instance of CategoryBean
     */
    public CategoryBean() {
    }
}
